package ch.hslu.gl20vscpudemo;

import java.nio.ByteBuffer;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.util.Log;

public class GLES20SurfaceView extends GLSurfaceView {

	// members ///////////////////////////////////////////////////////////////////
	private final Context mActivityContext;
	private GLES20Renderer mRenderer;

	// constructor ///////////////////////////////////////////////////////////////
	public GLES20SurfaceView(Context context, GLES20Renderer.Callback cb, int width, int height, float[][] kernel) {
		super(context);
		mActivityContext = context;

		// check if the system openGL ES 2.0 compatible
		if (detectOpenGLES20()) {
			// Tell the surface view we want to create an OpenGL ES 2.0-compatible
			// context, and set an OpenGL ES 2.0-compatible renderer.
			setEGLContextClientVersion(2); // Create an OpenGL ES 2.0 context.
			mRenderer = new GLES20Renderer(mActivityContext, cb, width, height, kernel);
			setRenderer(mRenderer);
			setRenderMode(RENDERMODE_WHEN_DIRTY);
		} else {
			// not compatible
			Log.e(getClass().toString(), "OpenGL ES 2.0 not supported on device.  Exiting...");
		}
	}

	// public methods ////////////////////////////////////////////////////////////
	public void pushImage(ByteBuffer bbI){
		mRenderer.pushImage(bbI);
		requestRender();
	}

	// private methods ///////////////////////////////////////////////////////////
	private boolean detectOpenGLES20() {
		final ActivityManager am = (ActivityManager) mActivityContext.getSystemService(Context.ACTIVITY_SERVICE);
		final ConfigurationInfo info = am.getDeviceConfigurationInfo();
		return (info.reqGlEsVersion >= 0x20000);
	}
}
